package com.example.shop_2210.search

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shop_2210.R
import com.example.shop_2210.adapter.GoodsHomeAdapter
import com.example.shop_2210.databinding.ActivitySearchBinding

@Route(path = "/app/SearchActivity")
class SearchActivity : BaseActivity<ActivitySearchBinding,SearchViewModel>() {
    lateinit var goodsHomeAdapter: GoodsHomeAdapter

    var isNew = false
    var price = ""
    var sales = ""
    var keyword = ""

    override fun initData() {
        viewModel.searchSuccess.observe(this){
            if(it.code==200){
                goodsHomeAdapter.data.clear()
                goodsHomeAdapter.notifyDataSetChanged()
                if(it.data.total>0){
                    goodsHomeAdapter.data.addAll(it.data.list)
                    goodsHomeAdapter.notifyDataSetChanged()
                }else{
                    ToastUtils.showLong("没有搜索到数据")
                }
            }
        }

    }

    override fun initView() {
        goodsHomeAdapter = GoodsHomeAdapter()
        binding.rvSv.adapter = goodsHomeAdapter
        binding.rvSv.layoutManager = GridLayoutManager(this,2)

        //执行搜索操作
        binding.sv.setOnQueryTextListener(object:SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                //当点击回车键时触发
                if(query!=null){
                    keyword = query
                    //请求搜索数据
                    viewModel.search(0,query,10,isNew,1,price,sales)
                }
                return false
            }
            override fun onQueryTextChange(newText: String?): Boolean {
                //当文本发生变化时触发
                return false
            }
        })
        //点击新品再次查询
        binding.isNew.setOnClickListener {
            if(isNew){//当前为选中状态
                //修改为未选中状态
                isNew = false
                binding.isNew.setTextColor(Color.GRAY)
            }else{
                isNew = true
                binding.isNew.setTextColor(Color.RED)
            }
            viewModel.search(0,keyword,10,isNew,1,price,sales)
        }
        //点击价格再次查询
        binding.price.setOnClickListener {
            if(price==""){
                price = "asc"
                binding.priceIv.setImageResource(R.drawable.sort3)
            }else if(price=="asc"){
                price = "desc"
                binding.priceIv.setImageResource(R.drawable.sort2)
            }else{
                price = ""
                binding.priceIv.setImageResource(R.drawable.sort1)
            }
            binding.salesIv.setImageResource(R.drawable.sort1)
            sales = ""
            viewModel.search(0,keyword,10,isNew,1,price,sales)
        }
        //点击销量再次查询
        binding.sales.setOnClickListener {
            if(sales==""){
                sales = "asc"
                binding.salesIv.setImageResource(R.drawable.sort3)
            }else if(sales=="asc"){
                sales = "desc"
                binding.salesIv.setImageResource(R.drawable.sort2)
            }else{
                sales = ""
                binding.salesIv.setImageResource(R.drawable.sort1)
            }
            binding.priceIv.setImageResource(R.drawable.sort1)
            price = ""
            viewModel.search(0,keyword,10,isNew,1,price,sales)
        }

    }

    override fun getViewModel(): Class<SearchViewModel>  = SearchViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_search

}